package com.chhuang.ewcrm.string;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import org.apache.commons.lang3.StringUtils;


/**
 *
 * @ClassName: Dimension2CodeUtil
 * @Description: 二维码生成和解析
 * @author: 黄晨
 * @date: 2015-7-9
 * 
 */
public class Dimension2CodeUtil {
	private Dimension2CodeUtil(){};

	private static final String CHARACTER_SET = "UTF-8";
	private static final String SUFFIX = ".jpg";
	
	/**
	 * 生成二维码
	 * @param content 待转二维码的文字内容
	 * @param width 图片宽度
	 * @param height 图片高度
	 * @param characterSet  字符串编码
	 * @param suffix 二维码图片文件格式
	 * @param stream 图片文件输出流
	 */
	public static void encoderQRCode(String content,int width,int height,String characterSet,String suffix,OutputStream stream){
		if(StringUtils.isEmpty(content)){
			return;
		}
		try {
		     MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		     Map<EncodeHintType,String> hints = new HashMap<EncodeHintType,String>();
		     hints.put(EncodeHintType.CHARACTER_SET, StringUtils.isNotEmpty(characterSet)?characterSet:CHARACTER_SET);
		     BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		     MatrixToImageWriter.writeToStream(bitMatrix, StringUtils.isNotEmpty(suffix)?suffix:SUFFIX, stream);
		} catch (WriterException e) {
		     e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析二维码
	 * @param stream 待解析二维码图片的输入流
	 * @return
	 */
	public static Result decoderQRCode(InputStream stream){
		Result result = null;
		if(stream != null){
			try{
	             MultiFormatReader formatReader = new MultiFormatReader();
				 BufferedImage image = ImageIO.read(stream);
				 LuminanceSource source = new BufferedImageLuminanceSource(image);
				 Binarizer binarizer = new HybridBinarizer(source);
				 BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
				 result = formatReader.decode(binaryBitmap);
			}catch (NotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
//	public static void main(String[] args) {
//	    String content = "120605;cnblogs.c;中文";
//	    String path = "C:/Users/HuangChen/Desktop/新建文件夹/a.jpg";
//	    File file = new File(path);
//		
//	    try {
//			OutputStream stream = new FileOutputStream(file);
//			Dimension2CodeUtil.encoderQRCode(content, 400, 400, null, null, stream);
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
//		
//		try {
//			InputStream stream = new FileInputStream(file);
//			Result result = decoderQRCode(stream);
//			System.out.println("result = "+ result.toString());
//			System.out.println("resultFormat = "+ result.getBarcodeFormat());
//			System.out.println("resultText = "+ result.getText());
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
//	}
}
